package com.aipodcast.webbackend.controller;

import com.aipodcast.webbackend.config.model.ApiResponse;
import com.aipodcast.webbackend.entity.Courses;
import com.aipodcast.webbackend.entity.vo.CoursePageVo;
import com.aipodcast.webbackend.entity.vo.CourseVo;
import com.aipodcast.webbackend.service.ICoursesService;
import com.aipodcast.webbackend.service.IUsersService;
import com.aipodcast.webbackend.utils.ApiResponseUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author RedStar
 * @date 2025/09/24
 * @description
 */

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Resource
    private ICoursesService coursesService;

    @GetMapping("/getCourseList")
    public ApiResponse<CoursePageVo> getCourseList(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize) {
        return ApiResponseUtils.success(coursesService.getCourseList(pageNum, pageSize));
    }

    @GetMapping("/getUploadSignUrl")
    public ApiResponse<String> getUploadSignUrl() {
        return ApiResponseUtils.success(coursesService.getUploadSignUrl());
    }

    @PostMapping("/uploadImg")
    public ApiResponse<String> uploadImg(@RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
        return ApiResponseUtils.success(coursesService.uploadImg(file));
    }

    @PostMapping("/addCourse")
    public ApiResponse<String> addCourse(HttpServletRequest request, @RequestBody CourseVo courseVo) {
        Boolean success = coursesService.addCourse((String)request.getAttribute("userId"), courseVo);
        return success ? ApiResponseUtils.success("添加成功") : ApiResponseUtils.error("添加失败");
    }

//    @GetMapping("/{id}")
//    public ApiResponse<CourseVo> getCourse(@PathVariable String id) {
//        Courses course = coursesService.getCourse(id);
//        return ApiResponseUtils.success(convert(course));
//    }

    @PostMapping("/updateCourse")
    public ApiResponse<String> updateCourse(@RequestBody CourseVo courseVo) {
        Courses courses = new Courses();
        BeanUtils.copyProperties(courseVo, courses);
        Boolean success = coursesService.updateById(courses);
        return success ? ApiResponseUtils.success("更新成功") : ApiResponseUtils.error("更新失败");
    }

    @PostMapping("/deleteCourse")
    public ApiResponse<String> deleteCourse(@RequestBody CourseVo courseVo) {
        Courses courses = new Courses();
        BeanUtils.copyProperties(courseVo, courses);
        Boolean success = coursesService.removeById(courses);
        return success ? ApiResponseUtils.success("删除成功") : ApiResponseUtils.error("删除失败");
    }

    private CourseVo convert(Courses courses) {
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(courses, courseVo);
        return courseVo;
    }
}
